<template>
	<div>
		<el-row :gutter="15" class="">
<el-form ref="elForm" :model="dataForm" :rules="dataRule" size="small" label-width="100px" label-position="right"  :disabled="setting.readonly" >
<template v-if="!loading && formOperates">
			<el-col :span="24"    v-if="judgeShow('docid')" >
				<jnpf-form-tip-item  label="文档id"  
 prop="docid"  >
					<el-input  v-model="dataForm.docid" @change="changeData('docid',-1)"  
 placeholder="请输入"   :disabled="judgeWrite('docid')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('description')" >
				<jnpf-form-tip-item  label="描述"  
 prop="description"  >
					<el-input  v-model="dataForm.description" @change="changeData('description',-1)"  
 placeholder="请输入"   :disabled="judgeWrite('description')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('enabledmark')" >
				<jnpf-form-tip-item  label="有效标志"  
 prop="enabledmark"  >
					<el-input  v-model="dataForm.enabledmark" @change="changeData('enabledmark',-1)"  
 placeholder="请输入"   :disabled="judgeWrite('enabledmark')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('creatortime')" >
				<jnpf-form-tip-item  label="创建时间"  
 prop="creatortime"  >
					<jnpf-open-data  v-model="dataForm.creatortime" @change="changeData('creatortime',-1)"  
 readonly   :disabled="judgeWrite('creatortime')"   :style='{"width":"100%"}' type="currTime" >

                </jnpf-open-data>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('creatoruserid')" >
				<jnpf-form-tip-item  label="创建用户"  
 prop="creatoruserid"  >
					<jnpf-open-data  v-model="dataForm.creatoruserid" @change="changeData('creatoruserid',-1)"  
 readonly   :disabled="judgeWrite('creatoruserid')"   :style='{"width":"100%"}' type="currUser" >

                </jnpf-open-data>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('lastmodifytime')" >
				<jnpf-form-tip-item  label="修改时间"  
 prop="lastmodifytime"  >
					<el-input  v-model="dataForm.lastmodifytime" @change="changeData('lastmodifytime',-1)"  
 placeholder="系统自动生成"  readonly   :disabled="judgeWrite('lastmodifytime')"   :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('lastmodifyuserid')" >
				<jnpf-form-tip-item  label="修改用户"  
 prop="lastmodifyuserid"  >
					<el-input  v-model="dataForm.lastmodifyuserid" @change="changeData('lastmodifyuserid',-1)"  
 placeholder="系统自动生成"  readonly   :disabled="judgeWrite('lastmodifyuserid')"   :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('mainurl')" >
				<jnpf-form-tip-item  label="主地址"  
 prop="mainurl"  >
					<el-input  v-model="dataForm.mainurl" @change="changeData('mainurl',-1)"  
 placeholder="请输入"   :disabled="judgeWrite('mainurl')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('copyurl')" >
				<jnpf-form-tip-item  label="复路径"  
 prop="copyurl"  >
					<el-input  v-model="dataForm.copyurl" @change="changeData('copyurl',-1)"  
 placeholder="请输入"   :disabled="judgeWrite('copyurl')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			<el-col :span="12"    v-if="judgeShow('version')" >
				<jnpf-form-tip-item  label="版本号"  
 prop="version"  >
					<el-input  v-model="dataForm.version" @change="changeData('version',-1)"  
 placeholder="请输入"   :disabled="judgeWrite('version')"   clearable  :style='{"width":"100%"}'>

                </el-input>
                </jnpf-form-tip-item>
            </el-col>
			</template>
            <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
		</el-form>
		</el-row>
		<UserBox v-if="userBoxVisible" ref="userBox" @submit="submit" />
	</div>
</template>
<script>
import {getFormById} from '@/api/workFlow/FormDesign'
import request from '@/utils/request'
import {mapGetters} from "vuex";
import comMixin from '@/views/workFlow/workFlowForm/mixin';
import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { getDefaultCurrentValueUserId } from '@/api/permission/user'
import { getDefaultCurrentValueDepartmentId } from '@/api/permission/organize'
import { getDateDay, getLaterData, getBeforeData, getBeforeTime, getLaterTime } from '@/components/Generator/utils/index.js'
import { thousandsFormat } from "@/components/Generator/utils/index"
	export default {
        mixins: [comMixin],
        components: {},
		props: [],
		data() {
			return {
				visible: false,
				loading: false,
                formRef: 'elForm',
				setting:{},
				eventType: '',
				userBoxVisible:false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
                },
              //可选范围默认值
              ableAll:{
              },
				tableRows:{
				},
                currVmodel:"",
				dataForm: {
					id:'',
					flowId: '',
					status: 1,
					freeapproveruserid: '',
					flowUrgent:1,
									docid : undefined,
									description : undefined,
                                        enabledmark : 1,
										creatortime : undefined,
										creatoruserid : undefined,
										lastmodifytime : undefined,
										lastmodifyuserid : undefined,
									mainurl : undefined,
									copyurl : undefined,
									version : undefined,
            },
			tableRequiredData: {},
                dataRule:
            {
                            docid: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],



            },

            childIndex:-1,
                    isEdit:false,
                    interfaceRes: {
                        docid:[] ,
                        description:[] ,
                        enabledmark:[] ,
                        creatortime:[] ,
                        creatoruserid:[] ,
                        lastmodifytime:[] ,
                        lastmodifyuserid:[] ,
                        mainurl:[] ,
                        copyurl:[] ,
                        version:[] ,
            },
        }
        },
        computed: {
            formOperates() {
                return this.setting.formOperates
            },
            ...mapGetters(['userInfo'])
        },
        watch: {},
		created() {
            this.getFormById();
            if(this.dataForm.id == null || this.dataForm.id == '' && this.dataForm.id == undefined || this.dataForm.id == 0) {
                this.initDefaultData()
            }
        },
		mounted() {},
		methods: {
            changeData(model, index) {
                this.isEdit = false
                this.childIndex = index
                let modelAll = model.split("-");
                let faceMode = "";
                for (let i = 0; i < modelAll.length; i++) {
                    faceMode += modelAll[i];
                }
                for (let key in this.interfaceRes) {
                    if (key != faceMode) {
                        let faceReList = this.interfaceRes[key]
                        for (let i = 0; i < faceReList.length; i++) {
                            if (faceReList[i].relationField == model) {
                                let options = 'get' + key + 'Options';
                                if(this[options]){
                                    this[options]()
                                }
                                this.changeData(key, index)
                            }
                        }
                    }
                }
            },
            changeDataFormData(type, data, model,index,defaultValue) {
                if(!this.isEdit) {
                    if (type == 2) {
                        for (let i = 0; i < this.dataForm[data].length; i++) {
                            if (index == -1) {
                                this.dataForm[data][i][model] = defaultValue
                            } else if (index == i) {
                                this.dataForm[data][i][model] = defaultValue
                            }
                        }
                    } else {
                        this.dataForm[data] = defaultValue
                    }
                }
            },
            //初始化默认数据
            initDefaultData() {

            },
            dataAll(){
            },
            getFormById() {
                getFormById("507269530267977029").then(res => {
                    this.dataForm.flowId = res.data&&res.data.flowId
                    // this.encode = res.data&&res.data.encode
                })
            },
            selfGetInfo(dataForm){
                this.dataInfo(dataForm)
            },
            beforeSubmit(){
                const _data =this.dataList()
                return _data
            },
            selfInit(){
                this.dataAll()
            },
			exist() {
				let isOk = true
				for (let key in this.tableRequiredData) {
					if (this.dataForm[key] && Array.isArray(this.dataForm[key])) {
						for (let i = 0; i < this.dataForm[key].length; i++) {
							let item = this.dataForm[key][i]
							inner: for (let id in item) {
								let arr = this.tableRequiredData[key].filter(o => o.id === id) || []
								if (!arr.length) continue inner
								if (arr[0].required) {
									let msg = `${arr[0].name}不能为空`
									let boo = true
									if (arr[0].dataType === 'array') {
										boo = !this.jnpf.isEmptyArray(item[id])
									} else {
										boo = !this.jnpf.isEmpty(item[id])
									}
									if (!boo) {
										this.$message({
											message: msg,
											type: 'error',
											duration: 1000
										})
										isOk = false
										break
									}
								}
							}
						}
					}
				}
				return isOk
			},


            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dateTime(timeRule, timeType, timeTarget, timeValueData, dataValue) {
                let timeDataValue = null;
                let timeValue = Number(timeValueData)
                if (timeRule) {
                    if (timeType == 1) {
                        timeDataValue = timeValue
                    } else if (timeType == 2) {
                        timeDataValue = dataValue
                    } else if (timeType == 3) {
                        timeDataValue = new Date().getTime()
                    } else if (timeType == 4) {
                        let previousDate = '';
                        if (timeTarget == 1 || timeTarget == 2) {
                            previousDate = getDateDay(timeTarget, timeType, timeValue)
                            timeDataValue = new Date(previousDate).getTime()
                        } else if (timeTarget == 3) {
                            previousDate = getBeforeData(timeValue)
                            timeDataValue = new Date(previousDate).getTime()
                        } else {
                            timeDataValue = getBeforeTime(timeTarget, timeValue).getTime()
                        }
                    } else if (timeType == 5) {
                        let previousDate = '';
                        if (timeTarget == 1 || timeTarget == 2) {
                            previousDate = getDateDay(timeTarget, timeType, timeValue)
                            timeDataValue = new Date(previousDate).getTime()
                        } else if (timeTarget == 3) {
                            previousDate = getLaterData(timeValue)
                            timeDataValue = new Date(previousDate).getTime()
                        } else {
                            timeDataValue = getLaterTime(timeTarget, timeValue).getTime()
                        }
                    }
                }
                return timeDataValue;
            },
            time(timeRule, timeType, timeTarget, timeValue, formatType, dataValue) {
                let format = formatType == 'HH:mm' ? 'HH:mm:00' : formatType
                let timeDataValue = null
                if (timeRule) {
                    if (timeType == 1) {
                        timeDataValue = timeValue || '00:00:00'
                        if (timeDataValue.split(':').length == 3) {
                            timeDataValue = timeDataValue
                        } else {
                            timeDataValue = timeDataValue + ':00'
                        }
                    } else if (timeType == 2) {
                        timeDataValue = dataValue
                    } else if (timeType == 3) {
                        timeDataValue = this.jnpf.toDate(new Date(), format)
                    } else if (timeType == 4) {
                        let previousDate = '';
                        previousDate = getBeforeTime(timeTarget, timeValue)
                        timeDataValue = this.jnpf.toDate(previousDate, format)
                    } else if (timeType == 5) {
                        let previousDate = '';
                        previousDate = getLaterTime(timeTarget, timeValue)
                        timeDataValue = this.jnpf.toDate(previousDate, format)
                    }
                }
                return timeDataValue;
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));










                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
                    _dataAll.id = _dataAll.id
                this.dataForm = _dataAll
                this.isEdit = true
                this.dataAll()
                this.childIndex=-1
            },
        },
    }

</script>
